import pygame
# 定义一个敌人的精灵类
import random


class Enemy(pygame.sprite.Sprite):
    def __init__(self, name, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.time = 0
        # 跑步的帧
        self.run_index = 0
        # 定义当前的状态,默认是跑
        self.current_state = "running"
        # 移动距离
        self.move_len = 0
        # 是否向左移动
        self.left = True
        self.dead = False
        # 加载图片
        self.load_images()
        
        self.rect.x = x
        self.rect.y = y

    # 加载图片的方法
    def load_images(self):
        # 读取马里奥素材图片
        file = pygame.image.load("./img/graphics/enemies.png").convert_alpha()

        self.run_frame = []
        self.dead_frame = []
        # 跑步1
        self.run_frame.append(self.cut_image(file, 0, 16, 16, 16))
        # 跑步2
        self.run_frame.append(self.cut_image(file, 16, 16, 16, 16))
        # 踩扁
        self.dead_frame.append(self.cut_image(file, 32, 16, 16, 16))
        # 设置初始图片为站立姿态
        self.image = self.run_frame[0]
        # 初始化精灵的rect位置
        self.rect = self.image.get_rect()
        pass

    # 精灵类主要的更新方法
    def update(self, keys):
        # 帧的更新
        self.update_frame()

        # 更新位置
        self.random_move()

        # 更新状态
        self.change_state(keys)

    def random_move(self):
        if(self.current_state != "running"):
            pass
        # 生成一个随机数，如果是偶数就向左移动，如果是奇数就向右移动
        if(self.move_len > 0):
            self.move_len -= 2
            if(self.left):
                self.rect.x -= 2
            else:
                self.rect.x += 2
        else:
            num = random.randint(0, 1)
            if(num == 0):
                self.left = True
            else:
                self.left = False
            self.move_len = random.randint(50, 200)
        if(self.rect.x < 0 or self.rect.x > 768):
            self.left = not self.left
            self.dead = True

    # 定义有限状态机方法，来设置当前的状态，避免跳的过程中突然切换成走路状态
    def change_state(self, keys):
        current_time = pygame.time.get_ticks()
        if(self.current_state == "running"):
            if(self.dead):
                self.current_state = "dead"  # 死亡
                self.dead_time = current_time
                self.move_len = 0
        if(self.current_state == "dead"):
            if(current_time - self.dead_time > 300):  # 死亡时间超过300ms
                self.kill()  # 删除精灵

    # 定义帧更新的方法
    def update_frame(self):
        if(self.current_state == "running"):
            self.update_run_frame()
        if(self.current_state == "dead"):
            self.update_dead_frame()
        pass

    # 跑步的帧更新
    def update_run_frame(self):
        current_time = pygame.time.get_ticks()
        if(self.time == 0):
            self.time = current_time
        elif(current_time - self.time > 200):
            self.time = current_time
            self.count_run_index()
            temp = self.run_frame[self.run_index-1]

            self.image = temp

    # 还是之前的数数012/012的方法
    def count_run_index(self):
        self.run_index = self.run_index+1
        if(self.run_index == 2):
            self.run_index = 0

    # 死亡动画
    def update_dead_frame(self):
        temp = self.dead_frame[0]
        self.image = temp

    # 裁切图片（图片，裁切目标左上角x，裁切目标左上角y，裁切目标宽度，裁切目标高度）
    def cut_image(self, source, x, y, width, height):
        image = pygame.Surface([width, height])
        image.set_colorkey((0, 0, 0))
        # 此处增加了第4个参数，用于切透明图片出来
        image.blit(source, (0, 0), (x, y, width, height),
                   pygame.BLEND_ALPHA_SDL2)
        image = pygame.transform.scale(image, (width*2, height*2))

        return image

    # 将图片画在屏幕上
    def draw(self, screen):
        screen.blit(self.image, self.rect)
